<template>
	<div>
		<el-table stripe border show-overflow-tooltip="true" :data="tableData" style="width: 100%" size="mini">
			<el-table-column label="文件名" prop="originalFilename">
			</el-table-column>
			<el-table-column label="操作">
				<template #default="scope">
					<a @click="onDownloadHandler(scope.row)" style="display:inline-block" class="link-button">下载</a>
					<a @click="onDeleteHandler(scope.row)" style="display:inline-block;margin-left: 5px;"
						class="link-button">删除</a>
				</template>
			</el-table-column>
		</el-table>
		<div style="display: flex;align-items: center;width:100%;justify-content: center;">
			<el-upload drag :action="url" :show-file-list="false" :before-upload="beforeUpload" :headers="headers"
				:on-success="onSuccess" style="margin-top: 10px;">
				<i class="el-icon-upload">
				</i>
				<div class="el-upload__text">
					拖放文件 <em>单击上传</em>
				</div>
			</el-upload>
		</div>
	</div>

</template>

<script>
	import constants from '@/utils/constants.js';
    import restUtil from '@/utils/restUtil.js';
	export default {
		name: "AttachmentPage",
		props: ["currentId", "model"],
		data() {
			return {
				tableData: [],
				headers: {
					"X-Token": window.localStorage.getItem("token")
				},
				url: "",
			}
		},
		methods: {
			init() {
				if (this.currentId == undefined || this.currentId == null || this.currentId == "") {
					this.$alert("请先保存", '提示');
					this.$emit("closeDialogEvent", this);
					return;
				}
				var that = this;
				restUtil.getAction(this,"attachment/init?model=" + this.model + "&recordId=" + this.currentId)
					.then(
						function(response) {
							if (response.data.code == 1) {
								that.tableData = response.data.attachmentList;
							} else {
								that.$alert(response.data.message, '提示')
							}
						});
			},
			beforeUpload() {
				return new Promise((resolve) => {
					this.url = constants.ROOT_URL + "attachment/upload?model=" + this.model + "&recordId=" + this
						.currentId;
					resolve();
				})
			},
			onSuccess() {
				this.init();
			},
			onDownloadHandler(row) {
				var url = constants.ROOT_URL + "attachment/download?id=" + row.id + "&X-Token=" + this.$store.state.token;
				window.open(url);
			},
			onDeleteHandler(row) {
				var that = this;
				this.$confirm("确定删除吗?", '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					restUtil.post(that,"attachment/deleteAttachment?id=" + row.id)
						.then(
							function(response) {
								if (response.data.code == 1) {
									that.init();
								} else {
									that.$alert(response.data.message, '提示')
								}
							});
				}).catch(() => {

				});
			}
		},
	}
</script>

<style>
</style>
